<?php

namespace App\Http\Controllers\User;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Category;
use App\Models\Task;
use App\Models\Worktype;
use App\Models\User;
use App\Models\TaskProcess;
use App\Models\TaskComment;
use App\Models\TaskService;
use App\Models\Service;
use App\Repos\TaskRepo;
use App\Repos\DepartRepo;
use Session;
use DB;
use App\Services\Wechat;

use Image;

class TaskController extends Controller
{
    public function index( Request $request)
    {
        $wechat_user =  session('wechat.oauth_user.default');
        $where = $param = [];
        $cate = 99;
        $query = Task::query();
        
        
    	$query = $query->where($where);
        if($request->keyword) {
            $query = $query->where('code','like','%'.$request->keyword.'%');
        }

        $list = $query->orderBy('created_at','desc')
                    ->paginate(10);
        $status = $this->taskStatus();

        
        return view('front.task.index',compact('list','status','param','cate'));
        
    }

    public function create(Request $request)
    {      
        $view = 'add1';
        $cate = 1;
        $departs = DepartRepo::get();
        if($request->cate==2) {
            $view = 'add2';
            $cate = 2;
        }
        $worktypes = Worktype::all();
        return view('front.task.'.$view,compact('departs','cate','worktypes'));
    }   

   

    public function save(Request $request)
    {
    	$rs['status'] = false;      
        $data         = $request->except(['_token']);    
        
       
        try{
           $wechat_user    =  session('wechat.oauth_user.default');
           
           // $data['userinfo'] = json_encode(Wechat::getUserInfo($wechat_user->id));
           $data['openid'] = $wechat_user->id;
           $code = date('mdhis');           
           $data['status']   = 1 ;
           $data['code']     = $code;
           //dd($data);
           $data['pics']     = rtrim(array_get($data,'pics'),',');
           $flag = Task::create($data);

           // 模板消息
           $ret = TaskRepo::sendNewMsg($flag->toArray());
          
            if(!$flag) {
               
                return response()->json($rs);
            } 
           
       } catch(\Exception $e) {
           DB::rollBack();
           $rs['msg'] = $e->getMessage().'89';
           return response()->json($rs);
       }
    	
        
        $rs['status'] = true;
        $rs['msg']    = '工单提交成功';

           
    
        return response()->json($rs);  

    }

    


    public function show($id)
    {
        $user = [];
        $wechat_user     =  session('wechat.oauth_user.default');
        $where['openid'] = $wechat_user->id;
        $user = User::where($where)->first();


        $data = Task::withTrashed()->find($id); 
        $pics = [];
        if($data->pics){
            $pics = explode(',', $data->pics);
        }      
        return view('front.task.show',compact('data','pics','user'));
    }   

    
    
    /**
     * 完工
     */
    public function processShow(Request $request)
    {
       
       
        $data   = Task::find($request->task_id);
        $pics = [];
        if($data->pics){
            $pics = explode(',', $data->pics);
        }  
        return view('front.task.process-show',compact('data','pics'));
        
    }

    public function processResult(Request $request)
    {
        $rs['status'] = 'success';
        $rs['msg']    = '操作成功';
        $data = $request->except(['_token']);
        
        $flag = TaskRepo::complete($data);
        if($flag['status']==false) {
            $rs['status'] = 'danger';
            $rs['msg'] = $flag['msg'];
            return back()->with('rs',$rs);
        }
        return back()->with('rs',$rs);
    }


    public function comment(Request $request)
    {
        $point_arr = $this->commentPoint();
       
        $data = $request->except(['_token']);
        $wechat_user =  session('wechat.oauth_user.default');  
        $data['user_openid'] = $wechat_user->id;

        $taskProcess = TaskProcess::where(['task_id'=>$request->task_id])->first();

        // 如果是零分那么要打回给维修人员
        if($data['point']==0) {
            //dd($request->task_id);
            $task = Task::find($request->task_id);
            $task->status = 50;
            $ret = $task->save();
            //dd($ret);
            $flag = TaskProcess::where('id',$taskProcess->id)->update(['status'=>50]);
            // 推送消息
             

            if($ret) {
                $rs['status'] = 'success';
                $rs['msg'] = '操作成功';
                return back()->with('rs',$rs);
            } 
        }


        $data['worker_id'] = $taskProcess->worker_id;
        $data['remark'] = $point_arr[$data['point']];

        $flag = TaskComment::create($data);
        //dd($flag);
        if($flag) {
            $rs['status'] = 'success';
            $rs['msg'] = '操作成功';
            return back()->with('rs',$rs);
        } 

        
        return back()->with('rs',$rs);
    }

    

}
